<?php
namespace app\beautiful\controller\admin\miniapp;
use app\beautiful\model\miniapp\RecommendAppModel;
use app\BaseController;
use app\common\logic\DataLogic;

class Recommend extends BaseController{
    protected $name='推荐小程序';

    public function index()
    {
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
        ];
        $coupon=new RecommendAppModel();
        $order=['add_time'=>'desc'];
        $field="*";
        $append=['add_time_format'];
        $return = DataLogic::getDataList($coupon,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }

    public function show()
    {
        $recommend_app_id=$this->request->param('recommend_app_id/d',0);
        if($recommend_app_id==0){
            return $this->success([]);
        }
        if(null === $recommend_app= RecommendAppModel::find($recommend_app_id)){
            return $this->error($this->name.'不存在');
        }
        if($recommend_app->is_delete!=0){
            return $this->error($this->name.'不存在');
        }
        if($recommend_app->shop_id != $this->request->shopId){
            return $this->error($this->name.'不存在');
        }
        $detail=[
            'logo'=>$recommend_app->logo,
            'name'=>$recommend_app->name,
            'app_id'=>$recommend_app->app_id,
        ];
        return $this->success(['detail'=>$detail]);
    }

    public function save()
    {
        $recommend_app_id=$this->request->param('recommend_app_id/d',0);
        $name=$this->request->param('name');
        $logo=$this->request->param('logo');
        $app_id=$this->request->param('app_id');
        if(empty($name) || mb_strlen($name)>20)  return $this->error('名称必须填写且长度不能大于20');
        if(empty($logo))  return $this->error('请上传logo');
        if(empty($app_id))  return $this->error('请填写小程序ID');

        if($recommend_app_id != 0){
            if(null === $recommend_app = RecommendAppModel::find($recommend_app_id)){
                return $this->error($this->name.'不存在');
            }
            if($recommend_app->is_delete != 0){
                return $this->error($this->name.'不存在');
            }
            if($recommend_app->shop_id != $this->request->shopId){
                return $this->error($this->name.'不存在');
            }
            $pre_logo=$recommend_app->logo;
        }else{
            $recommend_app = new RecommendAppModel();
            $recommend_app->shop_id = $this->request->shopId;
            $pre_logo=null;
        }

        $recommend_app->name=$name;
        $recommend_app->logo=$logo;
        $recommend_app->app_id=$app_id;
        $recommend_app->save();
        imgReplaceAct($logo,$pre_logo);
        return $this->success('保存成功');
    }

    public function del()
    {
        $recommend_app_id=$this->request->param('recommend_app_id/d',0);
        if($recommend_app_id==0){
            return $this->success([]);
        }
        if(null === $recommend_app= RecommendAppModel::find($recommend_app_id)){
            return $this->error($this->name.'不存在');
        }
        if($recommend_app->is_delete!=0){
            return $this->error($this->name.'不存在');
        }
        if($recommend_app->shop_id != $this->request->shopId){
            return $this->error($this->name.'不存在');
        }

        $recommend_app->is_delete = 1;
        $recommend_app->save();
        return $this->success('删除成功');
    }
}
